/**
 * 
 */
package com.iwords.service.persistence.impl;

import java.util.HashMap;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

import com.iwords.service.object.SysLibWordsEntity;
import com.iwords.service.persistence.SysLibWordsPersistence;

/**
 * @author 		BOLIN.HBC
 * @time 		2011-3-12
 * @description SysLibWords-PersistenceImpl
 */
@Stateless
public class SysLibWordsPersistenceImpl implements SysLibWordsPersistence {
	@PersistenceContext(unitName = "iwords")
	private EntityManager em;

	protected EntityManager getEntityManager() {
		return em;
	}

	public void create(SysLibWordsEntity entity) {
		getEntityManager().persist(entity);
	}

	public void edit(SysLibWordsEntity entity) {
		getEntityManager().merge(entity);
	}

	public void remove(SysLibWordsEntity entity) {
		getEntityManager().remove(getEntityManager().merge(entity));
	}

	public SysLibWordsEntity find(Object id) {
		return getEntityManager().find(SysLibWordsEntity.class, id);
	}

	public SysLibWordsEntity find(String email) {
		Query query = getEntityManager().createQuery("SELECT u from syslibwords u where u.email=?1");
		query.setParameter(1, email);
		try {
			return (SysLibWordsEntity) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<SysLibWordsEntity> findAll() {
		CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
		cq.select(cq.from(SysLibWordsEntity.class));
		return getEntityManager().createQuery(cq).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<SysLibWordsEntity> findRange(int[] range) {
		CriteriaQuery<Object> cq = getEntityManager().getCriteriaBuilder().createQuery();
		cq.select(cq.from(SysLibWordsEntity.class));
		javax.persistence.Query q = getEntityManager().createQuery(cq);
		q.setMaxResults(range[1] - range[0]);
		q.setFirstResult(range[0]);
		return (List<SysLibWordsEntity>) q.getResultList();
	}

	public int count() {
		CriteriaQuery<Object> cq = getEntityManager().getCriteriaBuilder().createQuery();
		javax.persistence.criteria.Root<SysLibWordsEntity> rt = cq.from(SysLibWordsEntity.class);
		cq.select(getEntityManager().getCriteriaBuilder().count(rt));
		javax.persistence.Query q = getEntityManager().createQuery(cq);
		return ((Long) q.getSingleResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	public List<SysLibWordsEntity> findWordsBySysLibId(Long sysLibId) {
		Query query = getEntityManager().createQuery("SELECT u from SysLibWordsEntity u where u.libid=?1");
		query.setParameter(1, sysLibId);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	public SysLibWordsEntity findWordByWordId(Long wordId) {
		Query query = getEntityManager().createQuery("SELECT u from SysLibWordsEntity u where u.wordid=?1");
		query.setParameter(1, wordId);
		try {
			return (SysLibWordsEntity) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<SysLibWordsEntity> getAllManagerWords(String sZiMu, int page) {
		Query query = getEntityManager().createQuery("SELECT u from SysLibWordsEntity u where  u.libid=900 order by u.spelling ").setFirstResult(page * 100)
				.setMaxResults(100);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}

	}

	// 获取下一个的单词
	@SuppressWarnings("unchecked")
	public java.util.Map<String, Object> getNextWord(Long wordid, int page, boolean isFirst) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("page", false);
		Query query = getEntityManager().createQuery("SELECT u from SysLibWordsEntity u where  u.libid=900 order by u.spelling ").setFirstResult(page * 100)
				.setMaxResults(100);

		// 获取这一页的所有单词
		List<SysLibWordsEntity> list = query.getResultList();

		// 代表这一页不存在
		if (list == null || list.size() == 0) {
			return null;
		}
		Long id = null;
		// 新的一页
		if (isFirst) {
			id = list.get(0).getWordid();
			map.put("id", id);
			map.put("page", true);
			return map;
		}

		// 匹配
		for (int i = 0; i < list.size(); i++) {

			if (list.get(i).getWordid().equals(wordid)) {
				if ((list.size() - 1) == i) {
					// 证明这是这一页的最后一个
					return getNextWord(wordid, page + 1, true);

				} else {
					id = list.get(i + 1).getWordid();
					map.put("id", id);
					return map;
				}
			}
		}
		map.put("id", 0L);
		return map;
	}

	public Long getAllManagerWordsCount() {
		Query query = getEntityManager().createQuery("SELECT count(u) from SysLibWordsEntity u where  u.libid=900  ");
		try {
			return (Long) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}

	}

	public void updateWords(SysLibWordsEntity word) {
		getEntityManager().createQuery("update SysLibWordsEntity l set   l.meaning= ?1,l.phonetics=?2 where l.wordid=?3 ").setParameter(1, word.getMeaning())
				.setParameter(2, word.getPhonetics()).setParameter(3, word.getWordid()).executeUpdate();

	}
}

 
